<?php
namespace app\model;

/**
 * Author: cbw
 * Desc: 微信投诉处理
 * Time: 2023/3/6 17:48
 * Email: 17854288794@163.com
 */
class Capabilities{


    public static function curl($url, $post = false,$header = [])
    {
        $headerArray = array_merge($header, [
            'Expect:',
        ]);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($post) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result ? json_decode($result, true) : [];
    }

    public static function decodeXml($xml)
    {
        if ($xml){
            libxml_disable_entity_loader(true);
            $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
            return $data;
        }
        return [];
    }

    /**
     * Desc:查询投诉单详情
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/6 17:59
     * @param $system
     * @param $complaintOrderId
     * @return array
     */
    public static function complaintOrderDetail($system,$complaintOrderId)
    {
        $access_token = Poster::getAccessToken($system);
        $url = "https://api.weixin.qq.com/wxaapi/minishop/complaintOrderDetail?complaintOrderId=$complaintOrderId&access_token=$access_token";
        $data = self::curl($url);
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[
                'complaintOrder'=>$data['complaintOrder'],
                'item'=>$data['item'],'returnBill'=>$data['returnBill']
            ]];
        }

    }

    /**
     * Desc:商家回应投诉
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/6 18:06
     * @param $system
     * @param $complaintOrderId
     * @param $content
     * @param int $bussiHandle
     * @return array
     */
    public static function bussiRespondComplaint($system,$complaintOrderId,$content,$bussiHandle=1)
    {
        $access_token = Poster::getAccessToken($system);
        $url = "https://api.weixin.qq.com/wxaapi/minishop/bussiRespondComplaint?access_token=$access_token";
        $params = [
            'content'=>$content,
            'complaintOrderId'=>$complaintOrderId,
            'bussiHandle'=>$bussiHandle
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }

    /**
     * Desc:商家补充凭证
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/6 18:08
     * @param $system
     * @param $complaintOrderId
     * @param $content
     * @return array
     */
    public static function bussiSupplyProof($system,$complaintOrderId,$content)
    {
        $access_token = Poster::getAccessToken($system);
        $url = "https://api.weixin.qq.com/wxaapi/minishop/bussiSupplyProof?access_token=$access_token";
        $params = [
            'content'=>$content,
            'complaintOrderId'=>$complaintOrderId,
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }

    /**
     * Desc:商家提交退款凭证
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2023/3/6 18:10
     * @param $system
     * @param $complaintOrderId
     * @param $content
     * @return array
     */
    public static function bussiSupplyRefund($system,$complaintOrderId,$content)
    {
        $access_token = Poster::getAccessToken($system);
        $url = "https://api.weixin.qq.com/wxaapi/minishop/bussiSupplyRefund?access_token=$access_token";
        $params = [
            'content'=>$content,
            'complaintOrderId'=>$complaintOrderId,
        ];
        $data = self::curl($url,json_encode($params),array('Content-Type: application/json'));
        if ($data['errcode']!=0){
            return ['code'=>$data['errcode'],'msg'=>$data['errmsg'],'data'=>[]];
        }else{
            return ['code'=>0,'msg'=>'ok','data'=>[]];
        }
    }


}